package pos.datos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import pos.datos.ConnectionManager;
import pos.dominio.Zonas;

public class JDBCZonasDAO implements IZonasDAO {


	


	public List selectAllZonas() {
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;
        
        try {

            String sql = "SELECT * FROM Zonas";
            stmt = conn.prepareStatement(sql); 
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            
                    Zonas temp = new Zonas();
                    temp.setIDzona(result.getString("IDZona"));
                    temp.setLocalizacion(result.getString("Localizacion"));
                    temp.setTamaño(result.getInt("Tamaño"));
                    temp.setHoraInicio(result.getTime("Hora_inicio"));
                    temp.setHoraFin(result.getTime("Hora_fin"));
                    temp.setDisponible(result.getBoolean("disponible"));
                    temp.setMapa(result.getString("mapa"));
                    temp.setIdlugar(result.getString("IDLugar"));
                    temp.setImagen(result.getString("imagen"));
                    temp.setCoordenadas(result.getString("Coordenadas"));
                    searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

	@Override
	
	
	public void cambiarDisponibilidad(String IDZona) {
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        String sql = "UPDATE Zonas SET disponible = 0 where ( IDZona = ? )";
        try {
            
            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, IDZona);
            stmt.executeUpdate();

          
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}
	public void disponible(String IDZona) {
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        String sql = "UPDATE Zonas SET disponible = 1 where ( IDZona = ? )";
        try {
            
            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, IDZona);
            stmt.executeUpdate();

          
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}
	
	public boolean TodoACero(String IDZona){
		
		boolean res=false;
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;
        int i=0;
        try {

            String sql = "SELECT * FROM horario WHERE (IDZona = ?)";
            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, IDZona);
            stmt.executeQuery();
            result = stmt.executeQuery();
            
            while(result.next()){
            	i=i+result.getInt("PlazasDisponibles");           	
            }
            if(i==0){
            	res=true;
            	cambiarDisponibilidad(IDZona);
            }else{
            	res=false;
            	disponible(IDZona);
            }

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return res;
	}
}
        
      
	
